package com.landmesure.utils;


import com.util.ByteUtil;

public class PropertyBuilderOfInteger implements PropertyBuilder {

	@Override
	public short[] buildProperty(int code, int len,String value) {
		short [] intByte = new short[len+1];
		intByte[0]=(short)code;
    	if (len==1){
    		intByte[1] = (short)Integer.parseInt(value);
    	}else if (len==2){
    		byte[] lvalue = ByteUtil.intToByte2(Integer.parseInt(value));
    		intByte[1] = ByteUtil.byteToShort(lvalue[1]);
    		intByte[2] = ByteUtil.byteToShort(lvalue[0]);
    	}
		return intByte;
	}

	@Override
	public String translateData(byte[] datas,int len,int offset) {
		String value =String.valueOf(ByteUtil.getIntNumber(offset, len, datas));
		return value;
	}

	@Override
	public String encode(String pvalue, int length) throws Exception {
		
		String result=null;
		switch (length){
        case 1:result= String.format("%02X", Long.parseLong(pvalue));break;
        case 2:result= String.format("%04X", Long.parseLong(pvalue));break;
        case 4:result= String.format("%08X", Long.parseLong(pvalue));
        }
		if (result==null&&length>0&&pvalue!=null){
			throw new Exception("整数的编码长度只能为[1,2,4]");
		}
		if (result.length()!=length*2){
			throw new Exception(String.format("整数[%s]超过了编码长度[%d]规定的范围",pvalue,length));
		}
		return result;
	}

	@Override
	public String decode(String pdata, int length) {
		return ((Long)Long.parseLong(pdata,16)).toString();
	}

	@Override
	public Long parseValue(String pvalue) {
		return Long.parseLong(pvalue);
	}
    public static void main(String[] args){
    	System.out.println(Long.parseLong("8000166A", 16));
    }
}
